package com.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.dao.MessageDao;
import com.model.Message;
import com.model.Users;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.service.MessageService;

public class MessageAction extends ActionSupport implements ModelDriven<Message>, ServletRequestAware {

	private Message message;
	private MessageService messageService;
	private HttpServletRequest request;

	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public String add() throws Exception {
		message.setTime(new Date().getDate()+"");
		Users u = (Users)request.getSession().getAttribute("user");
		System.out.println(u);
		message.setName(u.getUserName());
		messageService.save(message);
		return "success";
	}
	
	public String delete() throws Exception {
		System.out.println("message");
		messageService.delete(new Integer(request.getParameter("id")));
		return "success";
	}
	
	public String show() throws Exception {
		ArrayList<Message> list = messageService.getAll();
		request.setAttribute("messagelist",list);
		return "success";
	}
	
	public String update() {
		message =messageService.found(new Integer(request.getParameter("id")));
		return "success";
	}

	public String update2() {
		System.out.println(message);
		messageService.update(message);
		return "success";
	}
	
	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	@Override
	public Message getModel() {
		message = new Message();
		return message;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;		
	}
	

}
